package com.uyaer.engine.role
{
	import com.uyaer.engine.Const;
	import com.uyaer.engine.Engine;
	import com.uyaer.engine.core.Model;
	import com.uyaer.engine.display.Camera;
	import com.uyaer.engine.display.Icon;
	import com.uyaer.engine.event.EventManager;
	import com.uyaer.engine.event.EventType;
	import com.uyaer.engine.event.MyEvent;
	import com.uyaer.engine.utils.GetObject;
	import com.uyaer.engine.utils.HashMap;
	import com.uyaer.engine.utils.Point3D;
	import com.uyaer.engine.utils.Utils;
	import com.uyaer.engine.vo.GameObject;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class MyRole extends Model
	{
		private var isRuning:Boolean = false;
		private var isMoving:Boolean = false;
		private var isAttacking:Boolean = false;
		private var isFinding:Boolean = false;
		
		private var _type:int;
		private var moveSpeed:int = 6; //移动速度
		private var runSpeed:int = 12; //奔跑速度
		private var speed:int = 8; //速度
		private var jumpTop:int = 100; //跳跃高度
		private var oldDirX:int = 1;
		private var _dirX:int; //方向
		private var dirY:int; //方向
		private var index:int = 0;
		private var jumpV:Number = -25;
		private var vY:Number;
		private var g:Number = 5;
		private var isJumping:Boolean = false; //是否跳跃中
		private var isSkilling:Boolean = false; //是否在进行技能中 ，主要用于跳跃时中断技能
		
		
		public function MyRole(go:GameObject){
			super(go);
			
			dirX = 0;
			dirY = 0;
			
			EventManager.addEvent(EventType.ATTACK, _attackEvent);
			EventManager.addEvent(EventType.FIND, _findEvent);
			EventManager.addEvent(EventType.JUMP, _jumpEvent);
			EventManager.addEvent(EventType.MOVE, _moveEvent);
			EventManager.addEvent(EventType.RUN, _runEvent);
			EventManager.addEvent(EventType.STOP, _stopEvent);
			EventManager.addEvent(EventType.USESKILL, _useSkillEvent);
			EventManager.addEvent(EventType.SKILLOVER, _skillOverEvent);
		}
		
		/**
		 * 停止移动
		 * @param e
		 * 
		 */		
		private function _stopEvent(e:MyEvent):void{
			var dir:int = int(e.data);
			if(dir == Const.LEFT || dir == Const.RIGHT){
				dirX = 0;
			}else{
				dirY = 0;
			}
			if(dirX == 0 && dirY == 0){
				changeStateView(STAND_VIEW);
				isRuning = false;
				isMoving = false;
				EventManager.dispatchEvent(EventType.KEYBORDSTOP);
			}
		}
		
		/**
		 *技能完成后 
		 * @param e
		 * 
		 */		
		private function _skillOverEvent(e:MyEvent):void{
			isSkilling = false;
		}
		
		/**
		 *使用技能 
		 * @param e
		 * 
		 */		
		private function _useSkillEvent(e:MyEvent):void{
			if(!isJumping && !isSkilling){
				//播放技能
				isSkilling = true;
			}
		}
		
		/**
		 *奔跑函数 
		 * @param e
		 * 
		 */		
		private function _runEvent(e:MyEvent):void{
			var dir:Array = Utils.axis2Dir(int(e.data));
			if(dir[0]){
				dirX = dir[0];
			}
			if(dir[1]){
				dirY = dir[1];
			}
			this.speed = runSpeed;
			if(!isRuning){
				changeStateView(RUN_VIEW);
				isRuning = true;
				isMoving = false;
			}
		}
		
		/**
		 *主角移动 
		 * @param e
		 * 
		 */		
		private function _moveEvent(e:MyEvent):void{
			var dir:Array = Utils.axis2Dir(int(e.data));
			if(dir[0]){
				dirX = dir[0];
			}
			if(dir[1]){
				dirY = dir[1];
			}
			this.speed = moveSpeed;
			if(!isMoving){
				changeStateView(MOVE_VIEW);
				isMoving = true;
				isRuning = false;
			}
		}
		
		/**
		 *跳跃 
		 * @param e
		 * 
		 */		
		private function _jumpEvent(e:MyEvent):void{
			if(!isJumping){
				changeStateView(JUMP_VIEW);
				isJumping = true;
			}
		}
		
		/**
		 *拾取物品 
		 * @param e
		 * 
		 */		
		private function _findEvent(e:MyEvent):void{
			if(!isJumping && !isFinding){
				changeStateView(FIND_VIEW);
				isFinding = true;
				EventManager.dispatchEvent(EventType.MINE_FIND);
			}
		}
		
		/**
		 *进行攻击的处理函数 
		 * @param e
		 * 
		 */		
		private function _attackEvent(e:MyEvent):void{
			if(!isAttacking){
				changeStateView(ATTACK_VIEW);
				isAttacking = true;
				EventManager.dispatchEvent(EventType.MINE_ATTACK);
			}
		}
		
		/**
		 *每一帧进行的事件 
		 * 
		 */		
		override public function tick():void{
			super.tick();
			if(isJumping){
				if(index < 10){
					vY = jumpV + g * index;
					roleView.y += vY;
				}else{
					isJumping = false;
					changeStateView(STAND_VIEW);
					index = 0;
					roleView.y = 0;
				}
				index++;
			}
			if(dirX){
				var offX:Number = x + dirX * speed ;
				this.x = Math.min(Engine.instance.scene.maxWidth , Math.max(0,offX));
			}
			if(dirY && !isJumping){
				var d3y:Number = dirY * speed ;
				var offY:Number = y + Point3D.p3dTo2dY(d3y);
				this.y = Math.max(Engine.instance.scene.maxTop , Math.min(Camera.HEIGHT , offY));
				z_axis = y;
			}
			if(dirX || dirY){
				EventManager.dispatchEvent(EventType.MINE_POS_CHANGE,new Point(x,y));
			}
		}

		public function get dirX():int
		{
			return _dirX;
		}

		public function set dirX(value:int):void
		{
			_dirX = value;
			if(value != 0){
				oldDirX = value;
			}
		}

	}
}